/**
 * 
 */

package com.pp.admin.main.client.tab;

import java.util.Iterator;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.pp.admin.common.client.beans.Domain;
import com.pp.admin.common.client.i18.I18;
import com.pp.admin.common.client.service.Service;
import com.pp.admin.main.client.Administrator;

/**
 * @author pandurang
 */
public class AdminHeader extends HorizontalPanel implements Service, I18 {
	
	private AdminBasePage	parent		= null;
	
	private ListBox			domainList	= new ListBox();
	
	public AdminHeader( AdminBasePage parent ) {

		this.parent = parent;
		
		add( new Label( myConstants.selectdomain() + " : " ) );
		add( domainList );
		/**
		 * TODO: add progress circle when it retrieves domains list.
		 */
		domainService.getDomainsByUserID( Administrator.getCurrentUserInfo().getUserID(), new AsyncCallback<List<Domain>>() {
			
			@Override
			public void onSuccess( List<Domain> result ) {

				/**
				 * TODO: remove progress circle when it retrieves domains list.
				 */
				Domain domain = null;
				for ( Iterator<Domain> iterator = result.iterator(); iterator.hasNext(); ) {
					domain = iterator.next();
					domainList.addItem( domain.getUesrFriendlyName(), domain.getDomainID() );
				}
				AdminHeader.this.parent.onChange( null );
			}
			
			@Override
			public void onFailure( Throwable caught ) {

				/**
				 * TODO: remove progress circle when it retrieves domains list.
				 */
				
			}
		} );
		domainList.addChangeHandler( parent );
	}
	
	public String getSelectedDomainID() {

		return domainList.getValue( domainList.getSelectedIndex() );
	}
}
